package liang.jiaxin.com.utils;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.w3c.dom.Attr;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.InputStream;

/**
 * @User: Jrxsr
 * DomUtil
 * on 2019/5/9.
 **/
public class DomUtil {
    static DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    public String createDom(String openId,String MyId,String content) {
        Document document= DocumentHelper.createDocument();
        Element rootElm = document.addElement("XML");
        rootElm.addElement("ToUserName").addCDATA(openId);
        rootElm.addElement("FromUserName").addCDATA(MyId);
        rootElm.addElement("CreateTime").addCDATA(String.valueOf(System.currentTimeMillis()));
        rootElm.addElement("MsgType").addCDATA("text");
        rootElm.addElement("Content").addCDATA(content);
        //去掉头信息文件
        String xml=document.getRootElement().asXML();
        return xml;
    }
    public String resolvDom(InputStream ips){
        try{
            // 初始化xml解析工厂
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

            // 创建DocumentBuilder
            DocumentBuilder builder = factory.newDocumentBuilder();

            // 创建解析xml的Document
            org.w3c.dom.Document doc = builder.parse(ips);
            // 根节点名称
            String rootName = doc.getDocumentElement().getTagName();
            System.out.println("根节点: " + rootName);

            System.out.println("递归解析--------------begin------------------");
            // 递归解析Element
            org.w3c.dom.Element element = doc.getDocumentElement();
            parseElement(element);
            System.out.println("递归解析--------------end------------------");

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }


    public static void parseElement(org.w3c.dom.Element element){
        System.out.print("<" + element.getTagName());
        NamedNodeMap attris = element.getAttributes();
        for(int i = 0; i < attris.getLength(); i++) {
            Attr attr = (Attr) attris.item(i);
            System.out.print(" " + attr.getName() + "=\"" + attr.getValue() + "\"");
        }
        System.out.println(">");

        NodeList nodeList = element.getChildNodes();
        Node childNode;
        for (int temp = 0; temp < nodeList.getLength(); temp++) {
            childNode = nodeList.item(temp);

            // 判断是否属于节点
            if (childNode.getNodeType() == Node.ELEMENT_NODE) {
                // 判断是否还有子节点
                if(childNode.hasChildNodes()){
                    parseElement((org.w3c.dom.Element) childNode);
                } else if (childNode.getNodeType() != Node.COMMENT_NODE) {
                    System.out.print(childNode.getTextContent());
                }
            }
        }
        System.out.println("</" + element.getTagName() + ">");
    }
}
